package com.ea.admin.service.impl;

import com.ea.admin.mapper.UserInfoMapper;
import com.ea.admin.pojo.po.UserInfo;
import com.ea.admin.service.CompanyPermissionService;
import com.ea.admin.service.ReportService;
import com.ea.common.report.ReportFeign;
import com.ea.common.report.pojo.param.GetReportListParam;
import com.ea.common.report.pojo.vo.ReportDetailInfoVo;
import com.ea.common.report.pojo.vo.ReportSimpleInfoListVo;
import com.ea.common.result.BizException;
import com.ea.common.result.ErrorCode;
import com.ea.common.result.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ReportServiceImpl implements ReportService {
    @Resource
    UserInfoMapper userInfoMapper;
    @Resource
    CompanyPermissionService companyPermissionService;
    @Resource
    ReportFeign reportFeign;

    @Override
    public Long analyzeVideo(Long accountId, Long userId, String md5, Integer type) {
        // 检查用户是否存在
        UserInfo userInfo = userInfoMapper.selectUserInfoById(userId);
        if (userInfo == null) {
            throw new BizException(ErrorCode.SYSTEM_RESOURCE_ERROR_USER_NOT_EXISTS);
        }
        if (accountId != null) {
            // 是否拥有此用户的权限
            companyPermissionService.checkCompanyPermission(accountId, userInfo.getCompanyId());
        }

        //上传到服务器
        try {
            Result<Long> result = reportFeign.uploadVideo(
                    md5,
                    userId.toString(),
                    userInfo.getCompanyId().toString(),
                    type);
            if (!result.isSuccess()) {
                if (result.getCode() == ErrorCode.SYSTEM_ERROR.getCode()) {
                    throw new BizException(ErrorCode.TPS_DOWNSTREAM_SERVICE_ERROR);
                } else if (result.getCode() == ErrorCode.TPS_SA_ERROR.getCode()) {
                    throw new BizException(ErrorCode.TPS_SA_ERROR);
                } else if (result.getCode() == ErrorCode.TPS_FILE_UPLOAD_NO_FACE_ERROR.getCode()) {
                    throw new BizException(ErrorCode.TPS_FILE_UPLOAD_NO_FACE_ERROR);
                } else if (result.getCode() == ErrorCode.TPS_FILE_UPLOAD_ERROR.getCode()) {
                    throw new BizException(ErrorCode.TPS_FILE_UPLOAD_ERROR);
                } else if (result.getCode() == ErrorCode.TPS_FILE_UPLOAD_FACE_NO_CORRECT_ERROR.getCode()) {
                    throw new BizException(ErrorCode.TPS_FILE_UPLOAD_FACE_NO_CORRECT_ERROR);
                } else {
                    throw new BizException(ErrorCode.TPS_DOWNSTREAM_SERVICE_ERROR);
                }
            }
            return result.getData();
        } catch (BizException e) {
            throw e;
        } catch (Exception e) {
            throw new BizException(ErrorCode.TPS_DOWNSTREAM_SERVICE_ERROR);
        }
    }

    @Override
    public ReportSimpleInfoListVo getReportListByAccountId(Long accountId, Long userId, List<Integer> warningTypeList, List<Integer> statusList, String name, Date postStartDate, Date postEndDate, Date receiveStartDate, Date receiveEndDate, Date emoAnalyStartDate, Date emoAnalyEndDate, Date heartRateStartDate, Date heartRateEndDate, Integer page, Integer pageSize) {
        // 检查用户是否存在
        UserInfo userInfo = userInfoMapper.selectUserInfoById(userId);
        if (userInfo == null) {
            throw new BizException(ErrorCode.SYSTEM_RESOURCE_ERROR_USER_NOT_EXISTS);
        }
        // 是否拥有此用户的权限
        companyPermissionService.checkCompanyPermission(accountId, userInfo.getCompanyId());

        List<Long> userIdList = new ArrayList<>();
        userIdList.add(userId);
        Result<ReportSimpleInfoListVo> result = reportFeign.getReportList(new GetReportListParam(
                null,
                null,
                null,
                userIdList,
                warningTypeList,
                statusList,
                name,
                postStartDate,
                postEndDate,
                receiveStartDate,
                receiveEndDate,
                emoAnalyStartDate,
                emoAnalyEndDate,
                heartRateStartDate,
                heartRateEndDate,
                page,
                pageSize)
        );
        if (result.getCode() != ErrorCode.NO_ERROR.getCode()) {
            throw new BizException(ErrorCode.SYSTEM_ERROR);
        }
        return result.getData();
    }

    @Override
    public ReportDetailInfoVo getReportDetail(Long accountId, Long reportId) {
        // 检查权限
        Result<String> companyIdByReportId = reportFeign.getCompanyIdByReportId(reportId.toString());
        if (companyIdByReportId.getCode() != ErrorCode.NO_ERROR.getCode()) {
            throw new BizException(ErrorCode.SYSTEM_ERROR);
        }
        companyPermissionService.checkCompanyPermission(accountId, Long.parseLong(companyIdByReportId.getData()));

        Result<ReportDetailInfoVo> result = reportFeign.getReportDetail(reportId.toString());
        if (result.getCode() != ErrorCode.NO_ERROR.getCode()) {
            if (result.getCode() == ErrorCode.SYSTEM_RESOURCE_ERROR_REPORT_NOT_ANALYZED.getCode()) {
                throw new BizException(ErrorCode.SYSTEM_RESOURCE_ERROR_REPORT_NOT_ANALYZED);
            }
            if (result.getCode() == ErrorCode.SYSTEM_RESOURCE_ERROR_REPORT_NOT_EXISTS.getCode()) {
                throw new BizException(ErrorCode.SYSTEM_RESOURCE_ERROR_REPORT_NOT_EXISTS);
            }
            if (result.getCode() == ErrorCode.SYSTEM_RESOURCE_ERROR_REPORT_SA_ERROR.getCode()) {
                throw new BizException(ErrorCode.SYSTEM_RESOURCE_ERROR_REPORT_SA_ERROR);
            }
            throw new BizException(ErrorCode.TPS_DOWNSTREAM_SERVICE_ERROR);
        }
        if (result.getData() == null) {
            throw new BizException(ErrorCode.SYSTEM_RESOURCE_ERROR_REPORT_NOT_EXISTS);
        }
        return result.getData();
    }

//    @Override
//    public ReportDetailInfoVo getReportInfo(String userId, String md5) {
//
//        Result<ReportDetailInfoVo> result = reportFeign.getReportInfo(userId,md5);
//        if (result.getCode() != ErrorCode.NO_ERROR.getCode()) {
//            if (result.getCode() == ErrorCode.SYSTEM_RESOURCE_ERROR_REPORT_NOT_ANALYZED.getCode()) {
//                throw new BizException(ErrorCode.SYSTEM_RESOURCE_ERROR_REPORT_NOT_ANALYZED);
//            }
//            if (result.getCode() == ErrorCode.SYSTEM_RESOURCE_ERROR_REPORT_NOT_EXISTS.getCode()) {
//                throw new BizException(ErrorCode.SYSTEM_RESOURCE_ERROR_REPORT_NOT_EXISTS);
//            }
//            if (result.getCode() == ErrorCode.SYSTEM_RESOURCE_ERROR_REPORT_SA_ERROR.getCode()) {
//                throw new BizException(ErrorCode.SYSTEM_RESOURCE_ERROR_REPORT_SA_ERROR);
//            }
//            throw new BizException(ErrorCode.TPS_DOWNSTREAM_SERVICE_ERROR);
//        }
//        if (result.getData() == null) {
//            throw new BizException(ErrorCode.SYSTEM_RESOURCE_ERROR_REPORT_NOT_EXISTS);
//        }
//        return result.getData();
//    }


}
